package cn.thh.org.controller;

import cn.thh.basic.util.AjaxResult;
import cn.thh.basic.util.PageList;
import cn.thh.org.domain.Department;
import cn.thh.org.query.DepartmentQuery;
import cn.thh.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 编写部门业务接口
 * 1.返回类型
 * 2.接口做什么事情
 * 3.需要什么参数
 */

/*@GetMapping
@PostMapping
@DeleteMapping
@PutMapping*/

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;

    /**
     * 查询用户
     * @param id
     * @return
     */
    @GetMapping("{id}")//http://127.0.0.1:8080/dept/1  http://127.0.0.1:8080/dept/1
    @ApiOperation(value = "查询部门数据",notes = "需要部门Id")
    public Department getById(@PathVariable("id") Long id){
        return departmentService.getById(id);
    }

    // 修改保存 @PutMapping  有没有id
    @PutMapping
    @ApiOperation(value = "添加或修改部门",notes = "如果有id就是修改，如果没有就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙！"+e.getMessage());
        }
    }



    // 删除   @DeleteMapping   "{id}"
    @DeleteMapping("{id}")
    public AjaxResult delById(@PathVariable("id") Long id){
        /*  第一种： 比较low
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("系统繁忙！"+e.getMessage());
        }
        */
        /*  被pass了
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult me = AjaxResult.me();
            me.setSuccess(false);
            me.setMessage("系统繁忙！"+e.getMessage());
            return me;
        }*/

        /*链式编程思维*/
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me()
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }

    // 查询所有  @GetMapping
    @GetMapping
    public List<Department> getAll(){
        return departmentService.getAll();
    }


    // 分页 + 高级  @PostMapping

    /**
     *    通过@RequestBody来获取请求体中的数据
     * @param query
     * @return
     */
    @PostMapping
    public PageList<Department> getPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me()
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }

    // 查询部门树
    @GetMapping("/deptTree")
    public List<Department> getDeptTree(){
        return departmentService.getDeptTree();
    }

}
